package com.jswlw.common.factory;

import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;

/**
 * @Author: zhangpei
 * @Date: 2018/6/25 13:56
 * @Description: YAML配置文件加载器工厂类
 */
public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        if (resource == null) {
            super.createPropertySource(name, resource);
        }

        try {
            YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
            return loader.load(resource.getResource().getFilename(), resource.getResource()).get(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
